package com.jinzhu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinzhu.bo.UsersBO;
import com.jinzhu.emums.Sex;
import com.jinzhu.mapper.UsersMapper;
import com.jinzhu.pojo.Users;
import com.jinzhu.service.UserService;
import com.jinzhu.utils.MD5Utils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class UserServiceImpl implements UserService {
    /**
     * 用户表mapper
     */
    @Autowired
    private UsersMapper usersMapper;

    /**
     * 用户头像固定地址
     */
    private  static final String HEAD_PORTRAIT_URL = "https://avatar.csdnimg.cn/8/6/0/1_hhjjjjjjh.jpg";
    /**
     * 全局唯一id
     */
    @Autowired
    private Sid sid;

    /**
     * 查询用户是否存在
     * Propagation.SUPPORTS 如果当前存在事务，则加入该事务；如果当前没有事务，则以非事务的方式继续运行。
     * @param userName
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Boolean queryUserNameIsExist(String userName) {

        Users users = usersMapper.selectOne(new QueryWrapper<Users>()
                .lambda()
                .eq(Users::getUsername, userName));
        return users == null ? true : false ;
    }

    /**
     * 注册添加用户
     * Propagation.REQUIRED 如果当前存在事务，则加入该事务；如果当前没有事务，则创建一个新的事务。这是默认值。
     * @param usersBO
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Users insertUser(UsersBO usersBO) {
        Users users = new Users();
        users.setUsername(usersBO.getUsername());
        try {
            users.setPassword(MD5Utils.getMD5Str(usersBO.getPassword()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //默认用户昵称是用户名
        users.setNickname(usersBO.getUsername());
        //用户头像是固定地址
        users.setFace(HEAD_PORTRAIT_URL);
        //用户手机号
        users.setMobile("18598933274");
        //出生日期
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        users.setBirthday( LocalDateTime.parse("2017-09-08 00:00:00",df));
        //创建日期
        users.setCreatedTime(LocalDateTime.now());
        //修改日期
        users.setUpdatedTime(LocalDateTime.now());
        //邮箱
        users.setEmail("2324594295@qq.com");
        //姓名
        users.setRealname("小红");
        //性别
        users.setSex(Sex.man.type);
        //全局唯一id
        users.setId(sid.nextShort());

        usersMapper.insert(users);
        return users;
    }
    /**
     * 检索用户名和密码是否匹配，用于登录
     *   Propagation.SUPPORTS 如果当前存在事务，则加入该事务；如果当前没有事务，则以非事务的方式继续运行。
     * @param username
     * @param password
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users queryUserForLogin(String username, String password) {

//        try {
//            Thread.sleep(2500);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        Users users = usersMapper.selectOne(new QueryWrapper<Users>().lambda()
                .eq(Users::getUsername, username)
                .eq(Users::getPassword, password));
        return users;
    }
}
